package com.sundi.ecgeneralize.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecgeneralize.dto.CouponDto;
import com.sundi.ecgeneralize.dto.GrouponrulesDto;
import com.sundi.ecgeneralize.dto.ResultEntity;
import com.sundi.ecgeneralize.entity.EcCoupon;
import com.sundi.ecgeneralize.entity.EcGrouponRules;
import com.sundi.ecgeneralize.service.IEcGrouponRulesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 团购规则表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-26
 */
@Controller
@RequestMapping("/ecGrouponRules")
public class EcGrouponRulesController {

    @Autowired
    IEcGrouponRulesService ecGrouponRulesService;


    Logger logger = LoggerFactory.getLogger(this.getClass());

    //    查询
    @RequestMapping(value = "/grouponRulesList", method = RequestMethod.GET)
    public ResultEntity getConponList(GrouponrulesDto grouponrulesDto) {
        PageHelper.startPage(grouponrulesDto.getPageNum(), grouponrulesDto.getPageSize());
        List<EcGrouponRules> ecGrouponRules = ecGrouponRulesService.selectGrouponRulesList(grouponrulesDto);
        PageInfo<EcGrouponRules> info = new PageInfo<>(ecGrouponRules);
//        Wrapper<EcCoupon> wrapper = new EntityWrapper<EcCoupon>();
//        List<EcCoupon> ecCoupons = ecCouponService.selectList(wrapper);
//        logger.info(JSON.toJSONString(ecCoupons));
        return ResultEntity.ok(info);
    }


    //    添加
    @RequestMapping(value = "/addGrouponRules")
    public ResultEntity addCoupon(@RequestBody GrouponrulesDto grouponrulesDto) {
        System.out.println(grouponrulesDto);
        EcGrouponRules ecGrouponRules = new EcGrouponRules();
        BeanUtils.copyProperties(grouponrulesDto, ecGrouponRules);
        if (ecGrouponRules.getId() == null) {
            ecGrouponRules.setAddTime(new Date());
        }
        boolean insert = ecGrouponRulesService.insert(ecGrouponRules);
        return ResultEntity.ok(insert);
    }

    //    删除
    @RequestMapping(value = "/delGrouponRules")
    public ResultEntity del(Integer id) {
        System.err.println(id);
        ecGrouponRulesService.updAndDel(id);
        return ResultEntity.ok();
    }

    //回显
    @RequestMapping(value = "/getGrouponRules")
    public ResultEntity getCoupon(Integer id) {
        EcCoupon grouponRules = ecGrouponRulesService.getGrouponRules(id);
        System.err.println(grouponRules);
        return ResultEntity.ok(grouponRules);
    }
    //    修改
    @RequestMapping(value = "/updGrouponRules")
    public ResultEntity updCoupon(@RequestBody GrouponrulesDto grouponrulesDto) {
        System.out.println(grouponrulesDto);
        EcGrouponRules ecGrouponRules = new EcGrouponRules();
        BeanUtils.copyProperties(grouponrulesDto, ecGrouponRules);
        if (ecGrouponRules.getId() != null) {
            ecGrouponRules.setUpdateTime(new Date());
        }
        boolean b = ecGrouponRulesService.updateById(ecGrouponRules);
        return ResultEntity.ok(b);
    }
}
